package com.woniu108.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.exception.admin.AdminException;
import com.woniu108.carService.commos.exception.admin.AdminExceptionCode;
import com.woniu108.carService.commos.exception.produnt.ProductException;
import com.woniu108.carService.commos.exception.produnt.ProductExceptionCode;
import com.woniu108.product.client.PersonClient;
import com.woniu108.product.mapper.ServiceTypeMapper;
import com.woniu108.product.model.ServiceType;
import com.woniu108.product.param.DeleteServiceTypeParam;
import com.woniu108.product.param.InsertServiceTypeParam;
import com.woniu108.product.param.UpdateServiceTypeParam;
import com.woniu108.product.service.ServiceTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务类型表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class ServiceTypeServiceImpl extends ServiceImpl<ServiceTypeMapper, ServiceType> implements ServiceTypeService {
    @Resource
    private ServiceTypeMapper serviceTypeMapper;
    @Resource
    private PersonClient personClient;

    /**
     * 判断管理员
     */
    private void isAdmin(String adminId) {
        // 检查 adminId 是否为 null
        if (adminId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断管理员权限
        AdminInfoDto adminInfo = personClient.getByAdminId(adminId).getData();
        if (adminInfo == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_HAVE);
        }
        if (adminInfo.getAdminRoleId() != 1) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_PERMISSION);
        }
    }

    @Override
    public Boolean insertServiceType(InsertServiceTypeParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        // 检查服务类型名称是否为空
        if (param.getSTName() == null || param.getSTName().trim().isEmpty()) {
            throw new ProductException(ProductExceptionCode.SERVICE_TYPE_NAME_NULL);
        }

        // 查询是否存在该服务类型名称
        QueryWrapper<ServiceType> wrapper = new QueryWrapper<>();
        wrapper.eq("s_t_name", param.getSTName());
        ServiceType existingServiceType = serviceTypeMapper.selectOne(wrapper);
        // 判断服务类型名称是否已存在
        if (existingServiceType != null) {
            throw new ProductException(ProductExceptionCode.SERVICE_TYPE_NAME_EXISTS);
        }
        // 新增服务类型
        ServiceType newServiceType = new ServiceType();
        newServiceType.setSTName(param.getSTName());
        // 插入数据库
        int result = serviceTypeMapper.insert(newServiceType);
        return result > 0;

    }

    @Override
    public Boolean updateServiceType(UpdateServiceTypeParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        //查新名字是否存在
        QueryWrapper<ServiceType> wrapper = new QueryWrapper<>();
        wrapper.eq("s_t_name", param.getSTName());
        ServiceType selectOne = serviceTypeMapper.selectOne(wrapper);
        //判断名称是否存在
        if (selectOne != null) {
            throw new ProductException(ProductExceptionCode.SERVICE_TYPE_NAME_EXISTS);
        }
        ServiceType serviceType = serviceTypeMapper.selectById(param.getSTId());
        if (serviceType == null) {
            throw new ProductException(ProductExceptionCode.SERVICE_TYPE_NOT_HAVE);
        }
        //修改名称
        serviceType.setSTName(param.getSTName());
        int i = serviceTypeMapper.updateById(serviceType);
        return i > 0;
    }

    @Override
    public Boolean deleteServiceType(DeleteServiceTypeParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        // 根据ID查找服务种类
        ServiceType serviceType = serviceTypeMapper.selectById(param.getSTId());
        if (serviceType == null) {
            throw new ProductException(ProductExceptionCode.SERVICE_TYPE_NOT_HAVE);
        }

        // 删除服务种类
        int deleteCount = serviceTypeMapper.deleteById(param.getSTId());
        return deleteCount > 0;
    }
}
